package org.hxyjs.dao.impl;

import org.hxyjs.dao.UserSchemeRecordDao;

import org.hxyjs.entity.UserSchemeRecord;
import org.hxyjs.mapper.UserSchemeRecordMapper;
import org.hxyjs.page.PageInfo;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Repository("userSchemeRecordDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class UserSchemeRecordDaoImpl implements UserSchemeRecordDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private UserSchemeRecordMapper userSchemeRecordMapper;
    public List<UserSchemeRecord>  getAllUserSchemeRecords() throws Exception{ return userSchemeRecordMapper.selectByExample(null); }

    @Override
    public UserSchemeRecord getUserSchemeRecordByUno(String uno) throws Exception {
        return userSchemeRecordMapper.selectByPrimaryKey(uno); }

    @Override
    public List<UserSchemeRecord> getUserSchemeRecordsByPage(PageInfo pageInfo) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount =(int) userSchemeRecordMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return userSchemeRecordMapper.getUserSchemeRecordsByPage(map);
    }

    @Override
    public Integer deleteUserSchemeRecord(String uno) { return userSchemeRecordMapper.deleteByPrimaryKey(uno); }

    @Override
    public Integer updateUserSchemeRecord(UserSchemeRecord userSchemeRecord) { return userSchemeRecordMapper.updateByPrimaryKey(userSchemeRecord); }


    @Override
    public UserSchemeRecord getUserSchemeRecordsWithOrders(String uno) throws Exception {
        return null;
    }
    @Override
    public UserSchemeRecord getAccountByAccountName(String uno) throws Exception {
        return null;
    }

    @Override
    public Integer addUserSchemeRecordByUno(UserSchemeRecord userSchemeRecord){
        userSchemeRecord.setUsrno(initId());
        return userSchemeRecordMapper.insertSelective(userSchemeRecord);
    }
//
//    @Override
//    public UserSchemeRecord getUserSchemeRecordsWithOrders(String uno) throws Exception {
//        return userSchemeRecordMapper.getUserSchemeRecordsWithOrders(uno);
//    }
//    @Override
//    public UserSchemeRecord getAccountByAccountName(String uname) throws Exception {
//        return userSchemeRecordMapper.getAccountByAccountName(uname);
//    }
//

    public String initId() {

        String maxUsrno = userSchemeRecordMapper.getMaxUsrno();

        maxUsrno = Optional.ofNullable(maxUsrno).orElse("0000"); //如果maxUno 为空 就为0000
        return org.hxyjs.utils.GenNumTools.initId("us", 4, maxUsrno);
    }
}
